from typing import Dict

# 这里的单位 * 10 就是像素单位
字符高度 = 1.4
最大宽度 = 0.8
等宽字体 = True
宏前缀 = 'fast_draw_'
目标文件 = r'D:\Project\Top\Rmlog\lib\fast_draw_char.rml'

# 下面不要动了~~~

if not 等宽字体:
    width_bold = 最大宽度
    width_normal = 最大宽度 * 0.8
    width_fine = 最大宽度 * 0.6
    width_tiny = 最大宽度 * 0.4
else:
    width_bold = 最大宽度
    width_normal = 最大宽度
    width_fine = 最大宽度
    width_tiny = 最大宽度
height = 字符高度

# 字库
word = {}
word['space'] = f"""
{width_fine}
@10
"""
word['A'] = f"""
{width_normal}
0   4
4   6
@2
4   -6
-10
@10
0   -4
"""
word['B'] = f"""
{width_normal}
0   10
8
0   -5
@-8
10
0   -5
@-10
10
"""
word['C'] = f'''
{width_normal}
@10 10
-10
0   -10
10
'''
word['D'] = f"""
{width_normal}
0   10
5
@1 -1
3   -1
@1  -1
0   -4
@-1 -1
-3 -1
@-1 -1
-5
@10
"""
word['E'] = f"""
{width_fine}
@7  5
-7
@10  5
-10
0   -10
10
"""
word['F'] = f"""
{width_fine}
0   10
10  
@-10    -4
7
@3  -6
"""
word['G'] = f"""
{width_normal}
@10  10
-10
0   -10
10
0   5
-4
@4 -5
"""
word['H'] = f"""
{width_normal}
0   10
@0  -5
10
@0  5
0   -10
"""
word['I'] = f"""
{width_tiny}
@0  10
10
@-5
0   -10
@-5
10
"""
word['J'] = f"""
{width_fine}
@0  2
0   -2
10
0   10
-5
@5  -10
"""
word['K'] = f"""
{width_normal}
0   10
@10
-10 -5
10  -5
"""
word['L'] = f"""
{width_fine}
@0  10
0   -10
10
"""
word['M'] = f"""
{width_bold}
0   10
5   -5
5   5
0   -10
"""
word['N'] = f"""
{width_normal}
0   10
10  -10
@0  10
0   -10
"""
word['O'] = f"""
{width_normal}
@8
-6
@-2 1
0   8
@2  1
6
@2  -1
0   -8
@-2  -1
@2
"""
word['P'] = f"""
{width_fine}
0   10
10
0   -5
-10
@10 -5
"""
word['Q'] = f"""
{width_normal}
@7  2
0   8
-7
0   -8
7
3 -2
"""
word['R'] = f"""
{width_normal}
0   10
10
0   -5
-10
@4 -1
6   -4
"""
word['S'] = f"""
{width_fine}
10
0   5
-10
0   5
10
@0   -10
"""
word['T'] = f"""
{width_fine}
@5
0   10
@-5
10
@0  -10
"""
word['U'] = f"""
{width_fine}
@0  10
0   -10
10
0   10
@0  -10
"""
word['V'] = f"""
{width_normal}
@0  10
0   -4
4   -6
@2
4   6
0   4
@0  -10
"""
word['W'] = f"""
{width_bold}
@0  10
0   -10
5   5
5   -5
0   10
@0   -10
"""
word['X'] = f"""
{width_fine}
10  10
@-10
10  -10
"""
word['Y'] = f"""
{width_fine}
@0  10
5   -5
5   5
@-5 -5
0   -5
@5
"""
word['Z'] = f"""
{width_fine}
10  10
-10
@0  -10
10
"""

word['symbol_point'] = f"""
{width_tiny}
@2  2
>draw_poly(_brush_x,_brush_y,8,2,0)
@8  -2
"""
word['symbol_comma'] = f"""
{width_tiny}
4  4
@6  -4
"""
word['symbol_question'] = f"""
{width_fine}
@4  0
>draw_poly(_brush_x,_brush_y,8,2,0)
@1  4
0   2
5
0   4
-10
@10 -10
"""
word['symbol_colon'] = f"""
{width_tiny}
@4  2
>draw_poly(_brush_x,_brush_y,8,2,0)
@0  6
>draw_poly(_brush_x,_brush_y,8,2,0)
@6  -8
"""

word['0'] = f"""
{width_fine}
0   10
10
0   -10
-10
@5  4
0   2
@5   -6
"""
word['1'] = f"""
{width_fine}
@1
8
@-4
0   10
-4  -3
@9  -7
"""
word['2'] = f"""
{width_fine}
0   5
10
0   5
-10
@0  -10
10
"""
word['3'] = f"""
{width_fine}
10
0   10
-10
@0  -5
10
0   -5
"""
word['4'] = f"""
{width_fine}
@9
0   10
-9  -7
10
@0  -3
"""
word['5'] = f"""
{width_fine}
10
0   5
-10
0   5
10
@0  -10
"""
word['6'] = f"""
{width_fine}
@5  10
-5  -5
0   -5
10
0   5
-10
@10 -5
"""
word['7'] = f"""
{width_fine}
@5
5   10
-10
@10  -10
"""
word['8'] = f"""
{width_fine}
0   10
10
0   -10
-10
@0  5
10
@0  -5
"""
word['9'] = f"""
{width_fine}
@6
4   6
0   4
-10
0   -4
10
@0  -6
"""


def num(num: float, mul: float) -> str:
    return str(round(num * mul, 3))


# 转换一个字，例如 @0 2 \n 2 2 ，变成命令串的形式
def translate_one(name: str, code: str, line_height: int) -> str:
    result = []
    offset = [0, 0]
    lines = code.strip().split('\n')
    configs = [float(i) for i in lines[0].split(' ') if i]
    width = configs[0]
    for line in lines[1:]:
        line = line.strip()
        if line.startswith('#') or not line:
            continue
        if line.startswith('>'):
            result.append(line[1:].strip())
            continue

        func = 'draw_offset'
        if line.startswith('@'):
            line = line[1:].strip()
            func = 'move_offset'
        nums = [float(i) for i in line.split(' ') if i]

        if len(nums) == 1 or nums[1] == 0:
            offset[0] += nums[0]
            result.append(f'macro({func}_x,{num(nums[0], width)})')
            continue
        if nums[0] == 0:
            offset[1] += nums[1]
            result.append(f'macro({func}_y,{num(nums[1], line_height)})')
            continue
        offset[0] += nums[0]
        offset[1] += nums[1]
        result.append(f'macro({func},{num(nums[0], width)},{num(nums[1], line_height)})')

    if offset != [10, 0]:
        raise Exception(f'#! [{name}] offset: {offset}')

    return '\n'.join(result)


def translate(word_table: Dict[str, str], line_height: int, name_pre: str) -> str:
    result = []
    for k in word_table:
        res = translate_one(k, word_table[k], line_height).split('\n')
        res = ['\t' + i for i in res]
        res = '\n'.join(res)
        l = '{'
        r = '}'
        result.append(f'define({name_pre}{k},{l}\n{res}\n{r})')
    return '\n'.join(result)


res = translate(word, height, 宏前缀)
with open(目标文件, 'w', encoding='utf-8') as f:
    print(f"转换了 {len(word)} 个字符。")
    f.write(res)
    print(f"成功保存到了 {目标文件}")
